/**
 * Copyright (c) 2014-2016 www.nryuncang.com. All Rights Reserved.
 */
package com.topisv.tms.service.orderexport;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.mybatis.SqlMapDao;
import com.topisv.tms.repository.work.WorkOrderDao;
import com.topisv.tms.service.base.BaseService;

/**
 * 
 *
 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
 * @version 1.0.0
 */
@Component
@Transactional(readOnly = true)
public class OrderExportService extends BaseService {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private WorkOrderDao workOrderDao;
	@Autowired
	private SqlMapDao sqlMapDao;

	/**
	 * 分页查询
	 * 
	 * @param param
	 * @return
	 */
	public Page<WorkOrder> loadPageOrder(Map<String, Object> param, int pageNumber, int pageSize) {
		try {
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "id"));
			Specification<WorkOrder> spec = getWhereClause(param);

			Page<WorkOrder> page = workOrderDao.findAll(spec, pageRequest);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private Specification<WorkOrder> getWhereClause(final Map<String, Object> searchParams) {

		return new Specification<WorkOrder>() {

			@Override
			public Predicate toPredicate(Root<WorkOrder> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				Predicate pre = cb.conjunction();

				String customerCode = (String) searchParams.get("customerCode");
				if (StringUtils.isNotBlank(customerCode)) {
					pre.getExpressions().add(cb.equal(r.get("customerCode"), customerCode.trim()));
				}

				String customerCodes = (String) searchParams.get("customerCodes");
				if (StringUtils.isNotBlank(customerCodes)) {
					String[] customerCodeSplit = customerCodes.split(":");
					ComparisonPredicate[] tempCustomerCode = new ComparisonPredicate[customerCodeSplit.length];
					int i = 0;
					for (String _customerCode : customerCodeSplit) {
						tempCustomerCode[i] = (ComparisonPredicate) cb.equal(r.get("customerCode"), _customerCode);
						i++;
					}
					pre.getExpressions().add(cb.or(tempCustomerCode));
				}

				String warehouseCode = (String) searchParams.get("warehouseCode");
				if (StringUtils.isNotBlank(warehouseCode)) {
					pre.getExpressions().add(cb.equal(r.get("warehouseCode"), warehouseCode.trim()));
				}

				// 订单号
				String orderno = (String) searchParams.get("orderno");
				if (StringUtils.isNotBlank(orderno)) {
					pre.getExpressions().add(cb.equal(r.get("orderno"), orderno.trim()));
				}

				String status = (String) searchParams.get("status");
				if (StringUtils.isNotBlank(status)) {
					pre.getExpressions().add(cb.equal(r.get("status"), status.trim()));
				}

				Date fromDate = (Date) searchParams.get("fromDate");
				if (fromDate != null) {
					pre.getExpressions().add(cb.greaterThanOrEqualTo(r.<Date> get("createTime"), fromDate));
				}

				Date toDate = (Date) searchParams.get("toDate");
				if (toDate != null) {
					pre.getExpressions().add(cb.lessThanOrEqualTo(r.<Date> get("createTime"), toDate));
				}

				// 添加快递公司条件
				String cacode = (String) searchParams.get("expressCompany");
				if (StringUtils.isNotBlank(cacode)) {
					pre.getExpressions().add(cb.equal(r.<EdiOrder> get("order").get("cacode"), cacode));
				}

				String mailNo = (String) searchParams.get("mailNo");
				if (StringUtils.isNotBlank(mailNo)) {
					if ("0".equals(mailNo)) {
						pre.getExpressions().add(cb.and(cb.isNull(r.get("expressNo")), cb.isNull(r.get("ebillNo"))));
					}
					if ("1".equals(mailNo)) {
						pre.getExpressions()
								.add(cb.or(cb.isNotNull(r.get("expressNo")), cb.isNotNull(r.get("ebillNo"))));
					}
				}

				String cancelStatus = (String) searchParams.get("cancelStatus");
				if (StringUtils.isNotBlank(cancelStatus)) {
					pre.getExpressions().add(cb.equal(r.get("cancelStatus"), "ok"));
				}
				String batchCode = (String) searchParams.get("batchCode");
				if (StringUtils.isNotBlank(batchCode)) {
					pre.getExpressions().add(cb.equal(r.get("batchCode"), batchCode.trim()));
				}

				String taskCode = (String) searchParams.get("taskCode");
				if (StringUtils.isNotBlank(taskCode)) {
					pre.getExpressions().add(cb.equal(r.get("taskCode"), taskCode.trim()));
				}

				return pre;
			}
		};
	}
	
	/**
	 *查询作业订单信息
	 * 
	 *
	 * @param param
	 * @return [参数说明]
	 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
	 * @version 1.0.0
	 */
	public List<EdiOrder> exportOrderList (Map<String, Object> param){
		return sqlMapDao.selectList("ediOrderMapper.exportEdiOrderList", param);
	}
	
	/**
	 * 统计SKU种类数和商品数量
	 * 
	 *
	 * @param param
	 * @return [参数说明]
	 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
	 * @version 1.0.0
	 */
	public  List<EdiOrder> exportOrderItemNum (List<EdiOrder> orders){
		return  sqlMapDao.selectList("ediOrderMapper.exportOrderItemSkuNumAndGoodNum", orders);
	}
	
}
